package mytaxis.move.calendar;

import mytaxis.move.R;
import mytaxis.move.app.MyTaxisService;
import mytaxis.move.app.RequestReceivedActivity;
import mytaxis.move.model.MyTaxisModel;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.BroadcastReceiver;
import android.content.ComponentName;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.ServiceConnection;
import android.database.Cursor;
import android.os.Bundle;
import android.os.IBinder;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.SimpleCursorAdapter;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.AdapterView.OnItemClickListener;

public class PendingRequestsActivity extends Activity{
	
	private MyTaxisModel model;
	private MyTaxisService mBoundService;
	
	private TextView listEmpty;
	private ListView listTasks;
	
	private Cursor taskClicked;
	
	private ServiceConnection mConnection = new ServiceConnection() {
		public void onServiceConnected(ComponentName className, IBinder service) {
			mBoundService = ((MyTaxisService.LocalBinder)service).getService();
		}

		public void onServiceDisconnected(ComponentName className) {
			mBoundService = null;
		}
	};

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		this.setContentView(R.layout.requests);
		bindService(new Intent(this, MyTaxisService.class), mConnection, Context.BIND_AUTO_CREATE);	
		model = new MyTaxisModel(this);
		initViews();
		fillList();
	}

	private void initViews() {
		listEmpty = (TextView) findViewById(R.id.listEmpty);
		listTasks = (ListView) findViewById(R.id.ptlist);
		
		listTasks.setOnItemClickListener(new OnItemClickListener() {
			public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
				taskClicked = (Cursor) listTasks.getAdapter().getItem(position);
				AlertDialog.Builder alertbox = new AlertDialog.Builder(PendingRequestsActivity.this);
				alertbox.setMessage(getString(R.string.request_data)+"\n\n"+taskClicked.getString(2)+"\n"+
						taskClicked.getString(1)+"\n\n"+getString(R.string.accept_confirm));
	
				alertbox.setNegativeButton(R.string.reject,
						new DialogInterface.OnClickListener() {
							public void onClick(DialogInterface arg0, int arg1) {
								if(!model.pendingTaskExists(taskClicked.getString(0))){
									Toast.makeText(getApplicationContext(),R.string.pending_task_no_exists,0).show();
									fillList();
								}else{
									if(mBoundService.acceptRequest(false,taskClicked.getString(0))){
										model.deletePendingRequest(taskClicked.getString(0));
										fillList();
									}else{
										Toast.makeText(getApplicationContext(),R.string.connection_error,0).show();
									}
								}
							}
	
						});
	
				alertbox.setPositiveButton(R.string.accept,
						new DialogInterface.OnClickListener() {
							public void onClick(DialogInterface arg0, int arg1) {
								if(!model.pendingTaskExists(taskClicked.getString(0))){
									Toast.makeText(getApplicationContext(),R.string.pending_task_no_exists,0).show();
									fillList();
								}
								else{
									model.acceptPendingRequest(taskClicked);
									fillList();
								}
							}
						});
				alertbox.show();
			}
		});
	}
	
	private void fillList() {
		Cursor c = model.getPendingTasks();
		if (c.getCount() == 0) listEmpty.setText(getString(R.string.no_pending_tasks_stored));
		else listEmpty.setText("");
		String[] from = new String[] { "ubicacio", "datahorastring" };
		int[] to = new int[] { R.id.pname_row, R.id.pdate_row };
		SimpleCursorAdapter adapter = new SimpleCursorAdapter(this,
				R.layout.pendinglist_row, c, from, to);
		listTasks.setAdapter(adapter);
		startManagingCursor(c);
	}
	
	private BroadcastReceiver myReceiverSol = new BroadcastReceiver() {

		@Override
		public void onReceive(Context context, Intent intent) {
			Bundle extras = intent.getExtras();				
			String data = extras.getString(MyTaxisService.IMM_REQ_DATA);
			if(data.equals("1")){
				Intent i = new Intent(PendingRequestsActivity.this, RequestReceivedActivity.class);
				startActivity(i);
			}
		}
	};

	private void doRegister(){
		IntentFilter filter = new IntentFilter(MyTaxisService.IMM_REQ_FILTER);
		registerReceiver(myReceiverSol, filter);
	}

	private void doUnRegister(){
		unregisterReceiver(myReceiverSol);
	}
	
	@Override
	protected void onResume() {
		super.onResume();
		doRegister();
	}

	@Override
	protected void onPause() {
		super.onPause();
		doUnRegister();
	}


	@Override
	protected void onDestroy() {
		super.onDestroy();
		model.closeDB();
		unbindService(mConnection);
	}

}
